package com.woniu.permissions.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.exception.account.AccountException;
import com.woniu.exception.account.AccountExceptionCode;
import com.woniu.exception.permissions.PermissionsException;
import com.woniu.exception.permissions.PermissionsExceptionCode;
import com.woniu.permissions.mapper.AccountMapper;
import com.woniu.permissions.mapper.RoleMapper;
import com.woniu.permissions.model.Account;
import com.woniu.permissions.model.AccountRole;
import com.woniu.permissions.mapper.AccountRoleMapper;
import com.woniu.permissions.model.Role;
import com.woniu.permissions.param.AddAccountRoleParam;
import com.woniu.permissions.param.UpdateAccountRoleParam;
import com.woniu.permissions.service.AccountRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements AccountRoleService {

    @Resource
    private AccountRoleMapper accountRoleMapper;

    @Resource
    private AccountMapper accountMapper;
    @Resource
    private RoleMapper roleMapper;

    /**
     * 新增账号角色关联
     * @param toBean 账号id 角色id
     * @return
     */
    @Override
    public AccountRole addAccountRole(AddAccountRoleParam toBean) {
        Account account = accountMapper.selectById(toBean.getAccountId());
        if (account==null){
            throw new AccountException(AccountExceptionCode.ACCOUNT_NOT_REGISTER);
        }
        Role role = roleMapper.selectById(toBean.getRoleId());
        if (role==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NOT_ROLE);
        }
        AccountRole accountRole = new AccountRole(null, toBean.getAccountId(), toBean.getRoleId());
        accountRoleMapper.insert(accountRole);
        return accountRole;
    }
    /**
     * 修改账号角色关联
     * @param toBean 账号id 角色id
     * @return
     */
    @Override
    public AccountRole updateAccountRole(UpdateAccountRoleParam toBean) {
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id",toBean.getAccountId());
        AccountRole accountRole = accountRoleMapper.selectOne(wrapper);
        if (accountRole==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NOT_CAN);
        }
        Role role = roleMapper.selectById(toBean.getRoleId());
        if (role==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NOT_ROLE);
        }
        //修改角色
        accountRole.setRoleId(toBean.getRoleId());
        accountRoleMapper.updateById(accountRole);

        return accountRole;
    }
    /**
     * 查询账号角色关联
     * @param accountId 账号id
     * @return
     */
    @Override
    public AccountRole selectAccountRole(String accountId) {
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id",accountId);
        AccountRole accountRole = accountRoleMapper.selectOne(wrapper);
        if (accountRole==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NOT_CAN);
        }
        return accountRole;
    }
    /**
     * 根据账号id查询角色
     * @param accountId
     * @return
     */
    @Override
    public Role selectRoleByAccountId(String accountId) {
        System.out.println(accountId);
        System.out.println("=======================================");
        QueryWrapper<AccountRole> wrapper = new QueryWrapper<>();
        wrapper.eq("account_id",Long.valueOf(accountId));
        AccountRole accountRole = accountRoleMapper.selectOne(wrapper);
        if (accountRole==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NO_REPEAT);
        }
        Role role = roleMapper.selectById(accountRole.getRoleId());
        if (role==null){
            throw new PermissionsException(PermissionsExceptionCode.PERMISSIONS_EXCEPTION_CODE_NO_REPEAT);
        }
        return role;
    }

}
